package com.ruoyi.web.controller.warehouseManagement;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.domain.StoreBusiness;
import com.ruoyi.system.service.IStoreBusinessService;
import com.ruoyi.system.util.AbdulEmptyUtils;
import com.ruoyi.system.util.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 门店业务类型 控制器
 *
 * @author ruoyi
 * @date 2025-06-03
 */
@Api(tags = "门店仓储设置 - 门店业务类型")
@RestController
@RequestMapping("/store-business")
public class StoreBusinessController extends BaseController {

    @Autowired
    private IStoreBusinessService storeBusinessService;

    /**
     * 分页查询门店业务类型
     */
    @ApiOperation("分页查询门店业务类型")
    @PostMapping("/page")
    public BaseResponse<IPage<StoreBusiness>> page(@RequestBody StoreBusiness storeBusiness) {
        if (AbdulEmptyUtils.isEmpty(storeBusiness.getBusinessType())){
            throw new RuntimeException("请选择业务类型");
        }
        if (AbdulEmptyUtils.isEmpty(storeBusiness.getStoreId())){
            throw new RuntimeException("请选择门店");
        }
        Page<StoreBusiness> page = new Page<>(storeBusiness.getPageNum(), storeBusiness.getPageSize());
        LambdaQueryWrapper<StoreBusiness> wrapper = Wrappers.lambdaQuery();
        wrapper.like(!AbdulEmptyUtils.isEmpty(storeBusiness.getName()),StoreBusiness::getName, storeBusiness.getName());
        wrapper.eq(!AbdulEmptyUtils.isEmpty(storeBusiness.getStatus()),StoreBusiness::getStatus, storeBusiness.getStatus());
        wrapper.eq(StoreBusiness::getBusinessType, storeBusiness.getBusinessType());
        wrapper.eq(StoreBusiness::getStoreId, storeBusiness.getStoreId());
        IPage<StoreBusiness> result = storeBusinessService.page(page,wrapper);
        return BaseResponse.success(result);
    }


    /**
     * 新增或更新门店业务类型
     */
    @ApiOperation("新增或更新门店业务类型")
    @PostMapping("/saveOrUpdate")
    public BaseResponse<Boolean> saveOrUpdate(@RequestBody StoreBusiness storeBusiness) {
        if (AbdulEmptyUtils.isEmpty(storeBusiness.getBusinessType())){
            return BaseResponse.error("请选择业务类型");
        }
        if (AbdulEmptyUtils.isEmpty(storeBusiness.getName())){
            return BaseResponse.error("请输入业务名称");
        }
        if (AbdulEmptyUtils.isEmpty(storeBusiness.getStoreId())){
            return BaseResponse.error("请选择门店");
        }
        boolean result = storeBusinessService.saveOrUpdate(storeBusiness);
        return BaseResponse.success(result);
    }

    /**
     * 删除门店业务类型
     */
    @ApiOperation("删除门店业务类型")
    @DeleteMapping("/delete/{id}")
    public BaseResponse<Boolean> delete(@PathVariable Long id) {
        boolean result = storeBusinessService.removeById(id);
        return BaseResponse.success(result);
    }

    /**
     * 根据ID查询详情
     */
    @ApiOperation("根据ID查询门店业务类型详情")
    @GetMapping("/detail/{id}")
    public BaseResponse<StoreBusiness> detail(@PathVariable Long id) {
        StoreBusiness result = storeBusinessService.getById(id);
        return BaseResponse.success(result);
    }
}
